package net.skyedu.service.manager;

import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.skyedu.bean.SumLate;
import net.skyedu.bean.UserInfo;
import net.skyedu.dao.UserInfoDao;
import net.skyedu.utils.Page;
import net.skyedu.utils.PageHelp;

public class Manager extends HttpServlet
{
  private static final long serialVersionUID = 1684007998573742906L;

  public void destroy()
  {
    super.destroy();
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setContentType("text/html;charset=UTF-8");
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    HttpSession session = request.getSession();

    String USERID = request.getParameter("USERID");
    String userName = request.getParameter("username");
    String State = request.getParameter("state");
    int sta = Integer.parseInt(request.getParameter("sta"));
    String startTime = request.getParameter("startTime");
    String endTime = request.getParameter("endTime");
    String pagerMethod = request.getParameter("pagerMethod");
    String isqianhai=request.getParameter("isqianhai");
    System.out.println(startTime + endTime);
    if (pagerMethod != null) {
      startTime = (String)session.getAttribute("startTime");
      endTime = (String)session.getAttribute("endTime");
      userName = (String)session.getAttribute("uname");
    }
    int state = 0;
    if (State != null) {
      state = Integer.parseInt(State);
      session.setAttribute("state", Integer.valueOf(state));
    } else {
      Integer ss = (Integer)session.getAttribute("state");
      if (ss != null)
        state = ss.intValue();
    }
    int userId = 0;
    if (USERID != null) {
      userId = Integer.parseInt(USERID);
      session.setAttribute("manId", Integer.valueOf(userId));
    } else {
      Integer ser = null;
      if (session.getAttribute("manId") != null) {
        ser = (Integer)session.getAttribute("manId");
        userId = ser.intValue();
      } else {
        session.removeAttribute("manId");
        request.getRequestDispatcher("attend/error.jsp").forward(request, response);
        return;
      }
    }
    UserInfoDao userDao = new UserInfoDao();
    UserInfo ui = userDao.getSSN(userId);
    int totalRows = userDao.getCountBySchName(isqianhai,userId,ui.getDefaultDeptId(), userName, startTime, endTime, state, sta);
    Page page = PageHelp.getPager(request, totalRows);
    List attlist = userDao.getAttBySchName(isqianhai,userId,page.getPageSize(), page.getStartRow(), ui.getDefaultDeptId(), userName, startTime, endTime, state, sta);
    SumLate sum = userDao.getSum(userName, startTime, endTime, sta);
    if("1".equals(isqianhai))
    {
    	request.setAttribute("sele",1 );
    }
    else
    {
    	request.setAttribute("sele",0 );
    }
    request.setAttribute("userid", userId);
    request.setAttribute("masum", sum);
    request.setAttribute("page", page);
    request.setAttribute("attlist", attlist);
    request.setAttribute("len", Integer.valueOf(attlist.size()));
    request.setAttribute("mta", Integer.valueOf(sta));
    session.setAttribute("startTime", startTime);
    session.setAttribute("endTime", endTime);
    session.setAttribute("uname", userName);
    request.getRequestDispatcher("attend/backstage/manage.jsp").forward(request, response);
  }

  public void init()
    throws ServletException
  {
  }
}